package org.taskexecution.taskexecution.application.scheduledTask.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cephr.accountcenter.application.scheduledTask.TaskDefinitionService;
import com.cephr.accountcenter.infrastructure.presistent.mapper.scheduledTask.TaskDefinitionMapper;
import com.cephr.accountcenter.infrastructure.presistent.po.role.AppPO;
import com.cephr.accountcenter.infrastructure.presistent.po.role.MenuPO;
import com.cephr.accountcenter.infrastructure.presistent.po.scheduledTask.TaskDefinition;
import com.cephr.accountcenter.infrastructure.presistent.repository.workbench.AppRepository;
import com.cephr.accountcenter.infrastructure.presistent.repository.workbench.MenuRepository;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * @author Lydond
 * @description: 任务定义服务实现
 * @create_date: 2025/10/20-15:23
 */
@Service
public class TaskDefinitionServiceImpl extends ServiceImpl<TaskDefinitionMapper, TaskDefinition> implements TaskDefinitionService {


    @Resource
    AppRepository appRepository;

    @Resource
    MenuRepository menuRepository;

    @Override
    public List<TaskDefinition> gettaskDefinitionList() {
        LambdaQueryWrapper<TaskDefinition> wq = new LambdaQueryWrapper<>();
        List<TaskDefinition> taskDefinitions = this.list(wq);
        if (taskDefinitions == null || taskDefinitions.isEmpty()) {
            return Collections.emptyList();
        }
        //查询应用列表
        List<AppPO> allApps = appRepository.findAll();
        //查询菜单列表
        List<MenuPO> allMenus = menuRepository.findAll();

        taskDefinitions.forEach(taskDefinition -> {
            //设置应用名称
            allApps.stream().filter(appPO -> appPO.getAppId().equals(taskDefinition.getAppId())).findFirst()
                    .ifPresent(appPO -> taskDefinition.setAppName(appPO.getAppName()));
            //设置菜单名称
            allMenus.stream().filter(menuPO -> menuPO.getMenuId().equals(taskDefinition.getMenuId())).findFirst()
                    .ifPresent(menuPO -> taskDefinition.setMenuName(menuPO.getMenuName()));
        });

        return taskDefinitions;
    }
}

